<template>
	<view class="address">
		<u-swipe-action :show="res.show" :index="index" v-for="(res, index) in siteList" :key="res.id" @click="click"
			@open="open" :options="options" :disabled='disabled'>
			<view class="item">
				<view class="left" @click="getId(index)">
					<view class="top">
						<view class="name">{{res.name}}</view>
						<view class="phone">{{res.phone}}</view>
						<view class="tag">
							<text :class='res.is_default?"red":""'>{{res.is_default?'默认':''}}</text>
						</view>
					</view>
					<view class="bottom">
						{{res.province}}{{res.city}}{{res.county}}
					</view>
				</view>
				<view class="right">
					<u-icon name="edit-pen" :size="40" color="#999999" @click="edit(res.id)"></u-icon>
				</view>
			</view>
		</u-swipe-action>
		<view class="foot">
			<view class="addSite" @tap="toAddSite">
				<view class="add">
					<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
				</view>
			</view>
		</view>
		
		<!-- 删除 -->
		<u-modal v-model="show" :content="content" :show-cancel-button='true' @confirm='confirm'></u-modal>
	</view>
</template>

<script>
	import {
		addressList
	} from "../../common/http.api.js"
	export default {
		data() {
			return {
				activeIndex: '',
				show: false,
				content: '确认删除该地址?',
				siteList: [],
				disabled: true,
				btnWidth: 180,
				show: false,
				options: [{
						text: '默认',
						style: {
							backgroundColor: '#007aff'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			};
		},
		onLoad(options) {
			if (options.route == "pages/cart/orderPreview") {
				this.disabled = true
			} else {
				this.disabled = false
			}

			this.getData()
		},
		methods: {
			getId(index){
                uni.redirectTo({
                	url:'/pages/cart/orderPreview'
                })
				uni.setStorageSync('currentAddressId', JSON.stringify({id:this.siteList[index].id}))
			},
			async confirm() {
				uni.showLoading({
					title:'删除中',
					icon:'none',
				})
				const url = '/api/address/' + this.siteList[this.activeIndex].id;
				const res = await this.$u.delete(url);
				this.getData()
				uni.hideLoading()
			},
			async getData() {
				const res = await this.$u.api.addressList();
				res.data.data.forEach(item => {
					item.show = false;
				})
				this.siteList = res.data.data;
				console.log(this.siteList)
			},
			async click(index, index1) {
				this.activeIndex = index;
				if (index1 == 1) {
					this.show = true;
				} else {
 	               const url='/api/address/'+this.siteList[index].id+'/default';
				   const res=await this.$u.patch(url);
				   this.$u.toast(`设置默认地址成功`);
				   this.getData()
				}
	
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 	// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 	// 原本为'false'，再次设置为'false'会无效
				this.siteList[index].show = true;
				this.siteList.map((val, idx) => {
					if (index != idx) this.siteList[idx].show = false;
				})
			},
			toAddSite() {
				uni.navigateTo({
					url: '/pages/address/newaddress'
				})
			},
			edit(id) {
				const url = "/pages/address/newaddress?id=" + id
				uni.navigateTo({
					url
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.address{
		padding-bottom: 140rpx;
	}
	.item {
		margin: 20rpx;
		height: 120rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.left{
			flex: 1;
			.top {
				display: flex;
				.name {
					font-size: 36rpx;
					font-weight: bold;
				}
			
				.phone {
					font-size: 32rpx;
					margin: 0 20rpx;
				}
			
				.tag {
					display: flex;
					margin-top: 4rpx;
			
					text {
						display: block;
						width: 60rpx;
						height: 34rpx;
						line-height: 34rpx;
						font-size: 20rpx;
						border-radius: 6rpx;
						text-align: center;
						margin-left: 30rpx;
					}
			
					.red {
						background-color: red;
						color: #FFFFFF
					}
				}
			}
			
			.bottom {
				display: flex;
				justify-content: space-between;
			}
		}
	
	}
    .foot{
		width: 100%;
		height: 140rpx;
		background-color: #FFFFFF;
		line-height: 100rpx;
		position: fixed;
		bottom: 0rpx;
		left: 0rpx;
		display: flex;
		justify-content: center;
		.addSite {
			display: flex;
			justify-content: space-around;
			width: 600rpx;
			height: 100rpx;
			background-color: #007AFF;
			border-radius: 60rpx;
			font-size: 30rpx;
		
			.add {
				display: flex;
				align-items: center;
				color: #ffffff;
		
				.icon {
					margin-right: 10rpx;
				}
			}
		}
	}
	
</style>
